package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.bean.ResultDto;
import com.vehicle.mapper.CarMapper;
import com.vehicle.pojo.Btype;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Petrol;
import com.vehicle.pojo.PetrolRecord;
import com.vehicle.service.PetrolRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("petrolRecord")
public class PetrolRecordController extends DataTimeController{

    @Autowired
    private PetrolRecordService petrolRecordService;

    @RequestMapping("getCar")
    @ResponseBody
    public List<Car> findAllCar(){
        return petrolRecordService.findAllCar();
    }

    @RequestMapping("findAllPetrolRecord")
    @ResponseBody
    public DataGridView findAllPetrolRecord(Integer carid, PageVo pageVo){
        return petrolRecordService.findAllPetrolRecord(carid,pageVo);
    }

    @RequestMapping("deleteIds")
    @ResponseBody
    public ResultDto deleteAll(String idArray){
        ResultDto resultDto = petrolRecordService.deleteIds(idArray);
        return resultDto;
    }

    @RequestMapping("findAllBtype")
    @ResponseBody
    public List<Btype> findAllBtype(){
        return petrolRecordService.findAllBtypeByPetrol();
    }

    @RequestMapping("findFinalRecord")
    @ResponseBody
    public PetrolRecord findFinalRecord(Integer carid){
        return petrolRecordService.findFinalRecord(carid);
    }

    @RequestMapping("findAllPetrol")
    @ResponseBody
    public List<Petrol> findAllPetrol(){
        return petrolRecordService.findAllPetrol();
    }

    @RequestMapping("petrolRecordAdd")
    public ResultDto petrolRecordAdd(PetrolRecord petrolRecord){
        return petrolRecordService.petrolRecordAdd(petrolRecord);
    }

    @RequestMapping("petrolRecordEdit")
    public ModelAndView petrolRecordEdit(Integer petrolid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/car/petrolRecordEdit.jsp");
        PetrolRecord petrolRecord = petrolRecordService.findPetrolRecordById(petrolid);
        List<Petrol> petrol = petrolRecordService.findAllPetrol();
        List<Btype> allBtypeByPetrol = petrolRecordService.findAllBtypeByPetrol();
        modelAndView.addObject("btype",allBtypeByPetrol);
        modelAndView.addObject("petrol",petrol);
        modelAndView.addObject("petrolRecord",petrolRecord);
        return modelAndView;
    }

    @RequestMapping("doPetrolRecordEdit")
    @ResponseBody
    public ResultDto doPetrolRecordEdit(PetrolRecord petrolRecord){
        return petrolRecordService.updatePetrolRecord(petrolRecord);
    }

    @RequestMapping("PetrolRecordDelete")
    @ResponseBody
    public ResultDto PetrolRecordDelete(Integer petrolid){
        return petrolRecordService.deletePetrolRecord(petrolid);
    }
}
